package com.cloudpivot.receivales.ding.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.net.URI;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 钉钉项目 util类
 */
public class DingUtil {

    /**
     * list 转 ，号分隔的string
     * @param list
     * @return
     */
    public static String listToString(List list) {
        if (Objects.isNull(list)) {
            return null;
        }
        return StringUtils.join(list, ",");
    }

    /**
     * 编码url
     * @param str
     * @return
     */
    public static String urlEcode(String str) {
        try {
            String returnStr = new URI(null, str, null).toURL().toString();
            return returnStr;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 工具方法，获取obj内部
     *
     * @return
     */
    public static String getJsonInsideValue(JSONObject jsonObject, String key) {
        try {
            if (jsonObject == null) {
                return null;
            } else {
                return jsonObject.getString(key);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成guid
     */
    public static String getNewGUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    /**
     * 计算行数
     * @return
     */
    public static Integer calRow(String s) {
        try {
            if (StringUtils.isBlank(s)) {
                //空行
                return 2;
            }else{
                int r=s.length()/15;
                return r+1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }
}
